<?php
import( 'de.ceus-media.framework.neon.DefinitionView' );
import( 'classes.models.RightAction' );
/**
 *	Views of Right Actions.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_DefinitionView
 *	@uses			RightAction
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			12.02.2006
 *	@version		0.6
 */
/**
 *	Views of Right Actions.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_DefinitionView
 *	@uses			RightAction
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			12.02.2006
 *	@version		0.6
 *	@todo			Language Support of Messages
 *	@todo			Code Documentation
 */
class RightActionViews extends CMF_Krypton_DefinitionView
{
	/**
	 *	Builds Content Views.
	 *	@access		public
	 *	@return		void
	 */
	public function buildContent()
	{
		$request	= $this->ref->get( "request" );
		$auth		= $this->ref->get( 'auth' );

		$content	= "";
		if( $rightaction_id	= $request->get( 'rightaction_id' ) )
		{
#			if( $auth->hasRight( 'right_action', 'edit' ) )
				$content	.= $this->buildFieldEdit();
		}
		else
		{
#			if( $auth->hasRight( 'right_action', 'list' ) )
				$content	.= $this->buildFieldList();
#			if( $auth->hasRight( 'right_action', 'add' ) )
				$content	.= $this->buildFieldAdd();
		}
		return $content;
		
	}

	/**
	 *	Builds Control View from Wiki File.
	 *	@access		public
	 *	@return		string
	 */
	public function buildControl()
	{
		return $this->loadContent( "right_action_control.wiki" );
	}

	/**
	 *	...
	 *	@access		private
	 *	@return		string
	 */
	private function buildFieldAdd()
	{
		$config	= $this->ref->get( 'config' );
		$request	= $this->ref->get( 'request' );
		$words	= $this->words['right_action']['add'];

		$ui		= $this->buildForm( 'right_action', 'addAction', 'right_action', 'add' );

		$button_add		= $this->html->Button( 'addAction',		$words['button_add'] );

		$ui['field_button']		= $this->html->Field( '', $button_add );

		$ui['form']		= $this->html->Form( 'addAction', "?link=".$request->get( 'link' ) );
		$ui['colgroup']		= $this->html->ColumnGroup( "30%", "70%" );
		$ui['caption']		= $this->html->TableCaption( $words['caption_add'] );
		$ui['heading']		= $words['heading_add'];

		return $this->loadTemplate( "right.action.add", $ui );
	}

	/**
	 *	...
	 *	@access		private
	 *	@return		string
	 */
	private function buildFieldEdit()
	{
		$config	= $this->ref->get( 'config' );
		$request	= $this->ref->get( 'request' );
		$words	= $this->words['right_action']['edit'];

		if( $rightaction_id = $request->get( 'rightaction_id' ) )
		{
			$action	= new RightAction( $rightaction_id );
			$data	= $action->getData( TRUE );

			$ui		= $this->buildForm( 'right_action', 'editAction', 'right_action', 'edit', $data );

			$button_edit		= $this->html->Button( 'editAction',		$words['button_edit'] );
			$button_remove	= $this->html->Button( 'removeAction',	$words['button_remove'], 'buthot', $words['button_remove_confirm'] );

			$ui['field_buttons']		= $this->html->Field( '', $button_edit.$button_remove );

			$ui['form']		= $this->html->Form( 'editAction', "?link=".$request->get( 'link' )."&rightaction_id=".$request->get( 'rightaction_id' ) );
			$ui['colgroup']	= $this->html->ColumnGroup( "30%", "70%" );
			$ui['caption']		= $this->html->TableCaption( $words['caption_edit'] );
			$ui['heading']		= $words['heading_edit'];

			return $this->loadTemplate( "right.action.edit", $ui );
		}
		else
			$this->messenger->noteError( $words['msg_error1'] );
	}

	/**
	 *	...
	 *	@access		private
	 *	@return		string
	 */
	private function buildFieldList()
	{
		$config	= $this->ref->get( 'config' );
		$request	= $this->ref->get( 'request' );
		$auth	= $this->ref->get( 'auth' );
		$words	= $this->words['right_action']['list'];

		$editor	= $auth->hasRight( 'right_action', 'edit' );

		$list		= array();
		$action	= new RightAction();
		$actions	= $action->getAllData();
		if( count( $actions ) )
		{
			$i = 0;
			foreach( $actions as $action )
			{
				$label_desc	= $this->html->Label( '', $action['description'] );
				$style	= ++$i % 2 ? 'list1' : 'list2';
				if( $editor )
				{
					$link			= $this->html->Link( "?link=".$request->get( 'link' )."&rightaction_id=".$action['rightaction_id'], $action['title'] );
					$label_link	= $this->html->Label( '', $link );
				}
				else
					$label_link	= $this->html->Label( '', $action['title'] );
				$list[]	= "<tr class='".$style."'>".$label_link.$label_desc."</tr>";
			}
			$ui['actions']	= implode( "", $list);
		}
		else
			$ui['actions']	= "<tr>".$this->html->Label( '', $words['no_entries'] )."</tr>";

		$heads	= array(
			$words['head_action'],
			$words['head_description'],
		);
		$ui['heads']		= $this->html->TableHeads( $heads );
		$ui['colgroup']	= $this->html->ColumnGroup( "30%", "70%" );
		$ui['caption']		= $this->html->TableCaption( $words['caption_list'] );
		$ui['heading']		= $words['heading_list'];

		return $this->loadTemplate( "right.action.list", $ui );
	}
}
?>
